package Java7_Concurrency_Cookbook.concurrency_test.fork_join_test;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;

/**
 * @author lixiangke
 * @date 2017/12/4
 */
public class Main {
    public static void main(String[] args) throws InterruptedException {
        ForkJoinPool pool = new ForkJoinPool();
        int[] array = new int[10000];
        Task task = new Task(array, 0, array.length);
        pool.execute(task);
        while (!task.isDone()) {
            showLog(pool);
            TimeUnit.SECONDS.sleep(1);
        }
        pool.shutdown();
        pool.awaitTermination(1, TimeUnit.DAYS);
        showLog(pool);
        System.out.printf("Main: End of the program\n");
    }

    private static void showLog(ForkJoinPool pool) {
        System.out.printf("**********************\n");
        System.out.printf("Main: Fork/Join Pool log\n");
        // 为池建立的期望的并发级别
        System.out.printf("Main: Fork/Join Pool: Parallelism: %d\n", pool.getParallelism());
        // Fork/Join线程池中工作者线程的数量
        System.out.printf("Main: Fork/Join Pool: Pool Size: %d\n", pool.getPoolSize());
        // 当前正在执行任务的线程数
        System.out.printf("Main: Fork/Join Pool: Active Thread Count: %d\n", pool.getActiveThreadCount());
        // 当前正在工作且未在任何同步机制中被阻塞的线程数
        System.out.printf("Main: Fork/Join Pool: Running Thread Count: %d\n", pool.getRunningThreadCount());
        // 已经提交到线程池但未开始执行的任务数
        System.out.printf("Main: Fork/Join Pool: Queued Submission: %d\n", pool.getQueuedSubmissionCount());
        // 已经提交到线程池且已开始执行的任务数
        System.out.printf("Main: Fork/Join Pool: Queued Tasks: %d\n", pool.getQueuedTaskCount());
        // 线程池中是否有未开始执行的等待任务
        System.out.printf("Main: Fork/Join Pool: Has Queued Submissions: %s\n", pool.hasQueuedSubmissions());
        // 表示一个工作者线程从另一个工作者线程中偷得的任务的次数
        System.out.printf("Main: Fork/Join Pool: Steal Count: %d\n", pool.getStealCount());
        // Fork/Join池是否已经完成执行
        System.out.printf("Main: Fork/Join Pool: Terminated: %s\n", pool.isTerminated());
        System.out.printf("**********************\n");
    }
}
